<?php
require Yii::getPathOfAlias('application.project_common.vendor.submail') . '/SUBMAILAutoload.php';

/**
 *
 * @author smh <hi_shu@126.com>  Created on 2015-11-08
 */
class Sms extends MESSAGEXsend {

    protected $errcode = 0;
    protected $errmsg = '';

    public function __construct($comId) {
        $smsConfig=  SystemSms::findActiveModel($comId, 3);
        $config = [
            'appid' => $smsConfig->sms_user,
            'appkey' => $smsConfig->sms_pwd,
            'sign_type' => 'md5'
        ];//Yii::$app->params['submail']['sms'];
        //var_dump($config);
        parent::__construct($config);
    }

    public function sendVerifyCode($mobile, $code) {
        $this->setTo($mobile);
        $this->SetProject('SsLwu3');
        $this->AddVar('code', $code);
        $this->AddVar('time', '30分钟');

        $xsend = $this->xsend();
        return $this->parseResult($xsend);
    }
    
    /*
     * 
     */
    public function sendSms($mobile, $para, $templete) {
        $this->setTo($mobile);
        $this->SetProject($templete);
        foreach($para as $k=>$v)
        {
            $this->AddVar($k, $v);
        }
        //var_dump($this->Vars);
        $xsend = $this->xsend();
        //var_dump($xsend);
        return $this->parseResult($xsend);
    }
    
    protected function parseResult($res) {
        if ($res['status'] == 'success') {
            $this->errcode = 0;
            $this->errmsg = '';
            return false;
        }
        $this->errcode = $res['code'];
        $this->errmsg = $res['msg'];
        return true;
    }
}
